(function () {

    //获取显示页面数据 服务器分页
    var serverData = {};
    serverData.key = '';
    var serverTable;

    jQuery(document).ready(function () {

        serverTable = jQuery('#directionTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "post",
                "cache": false,
                "url":window.rootPath+ '/ui/direction/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                    data: null,
                    defaultContent: ""//排序
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate ='<input type="checkbox" class="checkchild">';
                        return operate;
                    }
                },
                {
                    data: 'activityName',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'address',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'user.empLogin.userName'
                },
                {
                	data:'user.saleArea.superiorArea'
                },
                {
                    data: 'activityDate',render : function(data,
                                                           type, full,
                                                           meta) {
                    if (data) {
                        return moment(
                            data)
                            .format(
                            "YYYY-MM-DD");
                    }
                }
                }],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });

        //添加活动信息，弹窗清空
        jQuery('.addBtn').on('click', function () {
            jQuery('#id').val('');
            jQuery('#activityName').val('');
            jQuery('#address').val('');
            jQuery('#activityDate').val('');
            jQuery('#directionModal').modal('show');
        });

        //编辑去向信息
        jQuery('.editDirectionBtn').on('click', function () {
            var selects=getSelect("#directionTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能编辑一条记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0) {
                $.springLemonDialog({
                    content: '请选择要编辑的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
            jQuery('#id').val(selects[0].id);
            jQuery('#activityName').val(selects[0].activityName);
            jQuery('#address').val(selects[0].address);
                if (selects[0].activityDate) {
                    var time = moment(selects[0].activityDate).format('YYYY-MM-DD');
                    var s1=jQuery('#activityDate').val(time);
                }
            jQuery('#directionModal').modal('show');
        };
        });

        //添加去向，保存
        var $btn = jQuery('.saveBtn').on('click', function () {
            if(jQuery("#directionEditForm") && !jQuery("#directionEditForm").Validform({tiptype:3, showAllError:true}).check(false)){
                return;
            };
            var isId=jQuery("#id").val();
            //判断是编辑还是新增
            if(isId == ""){
                var activityName = jQuery('#activityName').val();
                var address = jQuery('#address').val();
                var createTime=jQuery('#createTime').val();
                var activityDate = jQuery('#activityDate').val();
                var url = window.rootPath + "/ui/direction/save";
                var d = {
                    "user":{"id":$("#userId").val()},
                    "activityName": activityName,
                    "address": address,
                    "createTime":createTime,
                    "activityDate": activityDate
                };
                jQuery(".saveBtn").attr("disabled", true);
                jQuery.ajax({
                    type: 'post',
                    url: encodeURI(url),
                    data: JSON.stringify(d),
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    success: function (data) {
                        jQuery('#directionModal').modal('hide');
                        serverTable.draw(false)
                        jQuery(".saveBtn").attr("disabled", false);

                    }
                });

            }
            else{
                var data={
                    id:jQuery('#id').val(),
                    activityName:jQuery('#activityName').val(),
                    address:jQuery('#address').val(),
                    activityDate:jQuery('#activityDate').val()
                };
                var url=window.rootPath+'/api/directions/'+data.id;
                jQuery(".saveBtn").attr("disabled", true);
                jQuery.ajax({
                    url:encodeURI(url),
                    type:'put',
                    data:JSON.stringify(data),
                    dataType:'json',
                    contentType:'application/json;charset=utf-8',
                    success: function () {
                        jQuery('#directionModal').modal('hide');
                        serverTable.draw(false);
                        jQuery(".saveBtn").attr("disabled", false);
                    }

                });
            }
        });

        ///////checkbox全选
        $("#checkAll").click(function(){
            var check = $(this).prop("checked");
            $(".checkchild").prop("checked", check);
        });

        //查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        //撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });
        //删除去向
        jQuery('.delDirectionBtn').on('click', function () {
            var selects=getSelect("#directionTable",1);
            var url=window.rootPath+"/ui/direction/delDirection";
            delSelect(selects,url,serverTable)
        });


    });
})();

